package devicedbgui;

import devicedbgui.data.Build;
import devicedbgui.data.BuildDAO;
import devicedbgui.data.Device;
import devicedbgui.data.DeviceDAO;
import java.awt.CardLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/** Responsible for setup wizard used to set device and build information */
public class SetupWizard extends javax.swing.JDialog {

  /** Identifies whether using new or existing device */
  private int mode=-1;
  /** Object of DevicePanel class */
  DevicePanel devicePanel;
  /** Verifies whether wizard was completed */
  boolean cancel=false;
   /* Object of Device class */
    Device device = null;    
    /* Tests whether modifications made to database */
    public boolean isModifiedDB = false;
    /* Object of build class */
    public Build build = null;
    /** Reference to GUI class for accessing its variables */
    GUI gui;
    /** Identifies what component value has been clicked */
    int click=0;
    /** Stores device record */
     public String deviceRecord = "(select an item)";
     /* Stores build id */
     public String buildId = "(select an item)";
     /* Stores connection status */
     private Connection conn = null;
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
     /** Constructor creates for selecting and inputing device and build information    */
    public SetupWizard(GUI gui,DevicePanel devicePanel, Device device, boolean modal) {
        
         super(devicePanel.gui.frame, modal);
        this.devicePanel = devicePanel;
            this.gui=gui;
            this.conn=gui.connection;
        initComponents();
        
         if (device != null) // edit
        {
           
            this.device = device;
            fillDevice(device);
        }
        fillCombo();
        this.deviceRecord = gui.getDeviceRecord();
    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        MainPanel = new javax.swing.JPanel();
        p0 = new javax.swing.JPanel();
        newDev = new javax.swing.JRadioButton();
        existDev = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        p1 = new javax.swing.JPanel();
        txtRecord = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtMac = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSerial = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtType = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        p2 = new BuildPanel();
        txtBuildID = new javax.swing.JTextField();
        txtStart = new javax.swing.JTextField();
        txtEnd = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        p3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        cbDeviceRecord = new javax.swing.JList();
        dispBuilds = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        cbBuildId = new javax.swing.JList();
        addBuild = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        Cancel = new javax.swing.JButton();
        Next = new javax.swing.JButton();
        Back = new javax.swing.JButton();

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Device Wizard");
        setResizable(false);

        MainPanel.setLayout(new java.awt.CardLayout());

        newDev.setText("New Device");
        newDev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDevActionPerformed(evt);
            }
        });

        existDev.setText("Existing Device");
        existDev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                existDevActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Choose Device Type");

        javax.swing.GroupLayout p0Layout = new javax.swing.GroupLayout(p0);
        p0.setLayout(p0Layout);
        p0Layout.setHorizontalGroup(
            p0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p0Layout.createSequentialGroup()
                .addGroup(p0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p0Layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addGroup(p0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(existDev)
                            .addComponent(newDev, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(p0Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1))
                    .addGroup(p0Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)))
                .addContainerGap())
        );
        p0Layout.setVerticalGroup(
            p0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p0Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addComponent(newDev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(existDev)
                .addContainerGap(89, Short.MAX_VALUE))
        );

        MainPanel.add(p0, "1");

        jLabel2.setText("Record");

        jLabel4.setText("MAC");

        jLabel5.setText("Serial");

        jLabel6.setText("Name");

        jLabel7.setText("Type");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Set Device Information");

        javax.swing.GroupLayout p1Layout = new javax.swing.GroupLayout(p1);
        p1.setLayout(p1Layout);
        p1Layout.setHorizontalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p1Layout.createSequentialGroup()
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(p1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(p1Layout.createSequentialGroup()
                                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6)
                                    .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel5)))
                                .addGap(18, 18, 18)
                                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtSerial, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtMac, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(p1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE))
                    .addGroup(p1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        p1Layout.setVerticalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSerial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(27, 27, 27))
        );

        MainPanel.add(p1, "1_1");

        txtBuildID.setEditable(false);

        jLabel9.setText("End");

        jLabel10.setText("Start");

        jLabel11.setText("Build ID");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel8.setText("Set Device Information");

        javax.swing.GroupLayout p2Layout = new javax.swing.GroupLayout(p2);
        p2.setLayout(p2Layout);
        p2Layout.setHorizontalGroup(
            p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p2Layout.createSequentialGroup()
                .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p2Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtBuildID, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtStart, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(p2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                            .addGroup(p2Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel8)))))
                .addContainerGap())
        );
        p2Layout.setVerticalGroup(
            p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p2Layout.createSequentialGroup()
                        .addComponent(txtBuildID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(p2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addComponent(jLabel11))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        MainPanel.add(p2, "2_1");

        cbDeviceRecord.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        cbDeviceRecord.setModel(new javax.swing.DefaultComboBoxModel());
        jScrollPane1.setViewportView(cbDeviceRecord);

        dispBuilds.setText(">");
        dispBuilds.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dispBuildsActionPerformed(evt);
            }
        });

        cbBuildId.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        cbBuildId.setModel(new javax.swing.DefaultComboBoxModel());
        jScrollPane2.setViewportView(cbBuildId);

        addBuild.setText("Add");
        addBuild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBuildActionPerformed(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel12.setText("Set Device and Build");

        javax.swing.GroupLayout p3Layout = new javax.swing.GroupLayout(p3);
        p3.setLayout(p3Layout);
        p3Layout.setHorizontalGroup(
            p3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p3Layout.createSequentialGroup()
                .addGroup(p3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(p3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel12))
                    .addGroup(p3Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addGroup(p3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(dispBuilds, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(addBuild, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(p3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator5, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE))
        );
        p3Layout.setVerticalGroup(
            p3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(p3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p3Layout.createSequentialGroup()
                        .addComponent(dispBuilds)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addBuild, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        MainPanel.add(p3, "1_2");

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelactionPerformed(evt);
            }
        });

        Next.setText("Next");
        Next.setEnabled(false);
        Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }
        });

        Back.setText("Back");
        Back.setEnabled(false);
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(281, Short.MAX_VALUE)
                .addComponent(Back)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Next)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Cancel)
                .addGap(26, 26, 26))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {Back, Cancel, Next});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Next)
                    .addComponent(Back)
                    .addComponent(Cancel))
                .addGap(13, 13, 13))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   /**Displays devices in database */
    private void fillCombo() {
        DeviceDAO dao = new DeviceDAO(conn);
        try {
            ArrayList<Device> items = dao.selectAll();
            DefaultComboBoxModel model = (DefaultComboBoxModel) cbDeviceRecord.getModel();
            for (Device item : items) {
                model.addElement(item.record);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.toString(),
                    "Error 1", JOptionPane.ERROR_MESSAGE);
            return;
        }

    }
   /** Displays existing information in database */
    private void fillDevice(Device device)
    {
        txtRecord.setEditable(false);
        txtRecord.setText(device.record);
        txtMac.setText(device.MAC);
        txtSerial.setText(device.serial);
        txtName.setText(device.name);
        txtType.setText(device.type);
    }
    
    /** Returns whether new or existing device has been selected */
    public int getMode(){ return mode;}
    /** Returns id of current build */
    public String getBuild(){return buildId;}
    /* Haldles logic of going backwards in wizard */
private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
//determine previous card to display
    CardLayout cl = (CardLayout)(MainPanel.getLayout());
    //existing device panel
    if(card.equals("1_2")) {
        card="1";
        Back.setEnabled(false);
        Next.setText("Next");
        //build info panel
    }else if(card.equals("2_1")) {
        card="1_1";
        System.out.println("CARD: "+card);
        Back.setEnabled(false);
        Next.setText("Next");
        //device info panel
    }else if(card.equals("1_1")){
        card="1";
        
        Back.setEnabled(false);
        Next.setText("Next");
    }// initial panel
    else if (card.equals("1")){ 
        
        Next.setText("Next");}

    
    //show card
    cl.show(MainPanel, card);
   
}//GEN-LAST:event_BackActionPerformed
 String card = "1";
 
 /** Responsible for logic operations when next button is pressed
  */
private void NextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextActionPerformed
 //determine next card to display
    CardLayout cl = (CardLayout)(MainPanel.getLayout());
   
    System.out.println("card: "+ card); 
    //initial panel
    if(card.equals("1")) {
        if(mode==-1)    {   Next.setEnabled(false);}
     card = "1_" +String.valueOf(mode);
     
       if(mode==2)
       {Next.setText("Finish");}
     }//new device info panel
     else if(card.equals("1_1")) {
       //enable back button
       Back.setEnabled(true);
         card = "2_1";

          try {
         DeviceDAO dd = new DeviceDAO(devicePanel.gui.connection);
        if (device != null){
            device.MAC = txtMac.getText();
            device.serial = txtSerial.getText();
            device.name = txtName.getText();
            device.type = txtType.getText();
            dd.update(device);
        }
        else {
            device = new Device();
            device.record = txtRecord.getText();
            device.MAC = txtMac.getText();
            device.serial = txtSerial.getText();
            device.name = txtName.getText();
            device.type = txtType.getText();
            dd.insert(device);
       }
          gui.setDeviceRecord(device.record);
          
          }catch (Exception e){
        JOptionPane.showMessageDialog(new JFrame(), e.toString());
        return;
    }
         isModifiedDB = true;
         devicePanel.fillAllRecords();
     Next.setText("Finish");
    }//set new build info
     else if(card.equals("2_1")) {
       try {
          
        BuildDAO dd = new BuildDAO(devicePanel.gui.connection);
        if (build != null){
            String start = sdf.format(new Date());
            if (!txtStart.getText().equals("")) {
                        start = txtStart.getText();
                    }
            build.start = sdf.parse(start);
            
            String end = sdf.format(new Date());
            if (!txtEnd.getText().equals("")) {
                        end = txtEnd.getText();
                    }
            build.end = sdf.parse(end);
            
            build.Device_record = device.record;
            dd.update(build);
              
        }
        else {
            build = new Build();  
            String start = sdf.format(new Date());
            if (!txtStart.getText().equals("")) {
                        start = txtStart.getText();
                    }
            build.start = sdf.parse(start);
            
            String end = sdf.format(new Date());
            if (!txtEnd.getText().equals("")) {
                        end = txtEnd.getText();
                    }
            build.end = sdf.parse(end);
            
            build.Device_record = device.record;
            dd.insert(build);
            
               
        }
        
       
    }catch (Exception e){
        JOptionPane.showMessageDialog(new JFrame(), e.toString());
        return;
    }
    
    isModifiedDB = true;
    System.out.println("build:"+build.idBuild);
    dispose();
         
    }//Set info for existing device
     else if(card.equals("1_2")){
         
     if(cbBuildId.getSelectedIndex()!=-1)
     {
          buildId=cbBuildId.getModel().getElementAt(cbBuildId.getSelectedIndex()).toString();
          System.out.println("ID:"+buildId);
                   
     }
     else{JOptionPane.showMessageDialog(new JFrame(), "Select Build",
                    "Error 3", JOptionPane.ERROR_MESSAGE);}
     dispose();
     }
    
    //show card
    cl.show(MainPanel, card);
    
   
}//GEN-LAST:event_NextActionPerformed
/** Responsible for operations when cancel button is pressed
 * 
 * @param evt
 */
private void cancelactionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelactionPerformed
cancel=true;
   DeviceDAO dd = new DeviceDAO(devicePanel.gui.connection);
  if(card.equals("2_1")){
      try {
                dd.delete(device.record);
                
            } catch (Exception ex) {
                Logger.getLogger(SetupWizard.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
   gui.setDeviceRecord("(select an item)");
   gui.setBuildId("(select an item)");
    dispose();
}//GEN-LAST:event_cancelactionPerformed
/** Displays all builds associated with the current device */
private void dispBuildsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dispBuildsActionPerformed
 String chosen = cbDeviceRecord.getModel().getElementAt(cbDeviceRecord.getSelectedIndex()).toString();
 click=click+1;
 gui.setDeviceRecord(chosen);
    // get all builds associated with the selected device and fill the build combo box
    try {
        BuildDAO dao = new BuildDAO(conn);
        ArrayList<Build> items = dao.selectByDevice(chosen);
        DefaultComboBoxModel model = (DefaultComboBoxModel) cbBuildId.getModel();
        if(click>0)
        {
        model.removeAllElements();
        click=0;
        }
        for (Build item : items) {
            model.addElement(item.idBuild);
         }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.toString(),
                "Error 1", JOptionPane.ERROR_MESSAGE);
        return;
    }


}//GEN-LAST:event_dispBuildsActionPerformed
/** Asks the device to show setup wizard for existing devices */
private void existDevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_existDevActionPerformed
mode=2;
newDev.setSelected(false);
Next.setEnabled(true);
}//GEN-LAST:event_existDevActionPerformed

/** Adds new device to database when selected by user */
private void newDevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newDevActionPerformed
mode=1;
existDev.setSelected(false);
Next.setEnabled(true);
}//GEN-LAST:event_newDevActionPerformed
/** Adds a build to database when add button is clicked */
private void addBuildActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBuildActionPerformed
    EditBuildDlg dlg = new EditBuildDlg(gui, null, true);
    dlg.setVisible(true);
    click=click+1; 
    String chosen = cbDeviceRecord.getModel().getElementAt(cbDeviceRecord.getSelectedIndex()).toString();
    gui.setDeviceRecord(chosen);
    // get all builds associated with the selected device and fill the build combo box
    try {
        BuildDAO dao = new BuildDAO(conn);
        ArrayList<Build> items = dao.selectByDevice(chosen);
        DefaultComboBoxModel model = (DefaultComboBoxModel) cbBuildId.getModel();
        if(click>0)
        {
        model.removeAllElements();
        click=0;
        }
         for (Build item : items) {
            model.addElement(item.idBuild);
        
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.toString(),
                "Error 1", JOptionPane.ERROR_MESSAGE);
        return;
    }
    
}//GEN-LAST:event_addBuildActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back;
    private javax.swing.JButton Cancel;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JButton Next;
    private javax.swing.JButton addBuild;
    private javax.swing.JList cbBuildId;
    private javax.swing.JList cbDeviceRecord;
    private javax.swing.JButton dispBuilds;
    private javax.swing.JRadioButton existDev;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JRadioButton newDev;
    private javax.swing.JPanel p0;
    private javax.swing.JPanel p1;
    private javax.swing.JPanel p2;
    private javax.swing.JPanel p3;
    private javax.swing.JTextField txtBuildID;
    private javax.swing.JTextField txtEnd;
    private javax.swing.JTextField txtMac;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtRecord;
    private javax.swing.JTextField txtSerial;
    private javax.swing.JTextField txtStart;
    private javax.swing.JTextField txtType;
    // End of variables declaration//GEN-END:variables

}
